﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;


namespace CSharpHW
{
    public partial class Charts : Form
    {
        public Charts()
        {
            InitializeComponent();
        }

        private void Charts_Load(object sender, EventArgs e)
        {

            //comboBox默认值
            this.comboBox_year.SelectedIndex = 0;
            this.comboBox_month.SelectedIndex = 0;
            this.comboBox_chartType.SelectedIndex = 0;
            //chart默认值
            conditionChanged();

        }

        
        private DataTable createDt(string year_s, int month, int chartType)
        {
            int year = int.Parse(year_s);
            DataSet data;
            
            
            //
            if (month == 0)  data = DBUtility.getDataByYear(year);
            else data = DBUtility.getDataByMonth(year, month);


            DataTable dt = new DataTable();

            dt.Columns.Add("item", typeof(String));
            dt.Columns.Add("pay", typeof(double));

            //String[] items = { "一般", "饮食", "交通" };
            //double[] values = { 100, 10, 300 };

            dt.Rows.Add("一般", 5);
            dt.Rows.Add("饮食", 10);
            dt.Rows.Add("交通", 3);

            return dt;
        }

        private void conditionChanged()
        {

            DBUtility.connect("1234", "root");

            //get condition
            String year = this.comboBox_year.Text;
            int month = this.comboBox_month.SelectedIndex;
            int chartType = this.comboBox_chartType.SelectedIndex;

            DataTable dt = createDt(year, month, chartType);
            //绑定数据源
            chart1.DataSource = dt;
            chart1.DataBind();

            //
            chart1.Series[0].XValueMember = "item";
            chart1.Series[0].YValueMembers = "pay";
            //change chartType
            if (chartType == 3)
            {
                chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Pie;
            }
            else
            {
                chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
            }

            DBUtility.disconnect();
        }

        //when condition is changed
        private void comboBox_year_SelectedIndexChanged(object sender, EventArgs e)
        {
            conditionChanged();
        }

        private void comboBox_month_SelectedIndexChanged(object sender, EventArgs e)
        {
            conditionChanged();
        }

        private void comboBox_chartType_SelectedIndexChanged(object sender, EventArgs e)
        {
            conditionChanged();
        }
    }
}
